#include "Tile.h"

Tile::Tile()
{
    //初始化坐标
    x = 0;
    y = 0;
    nowFrameIndex = 0;
    //初始化速率
    xVel = 0;
    yVel = 0;
    setClips();
    filename = "../assert/tiles.png";
    background = loadImage(filename);

    for( int i = 0; i < GRID_RAW; i++ )
    {
        for( int j = 0; j < GRID_CAL; j++ )
        {
            grid[i][j] = rand() % 9;
            cout << grid[i][j] << " ";
        }
        cout << endl;
    }
    
}

Tile::~Tile()
{
    if( background != NULL )
    {
        SDL_FreeSurface( background ); 
    }
}


void Tile::move()
{
    //将圆点左右移动
    x += xVel;
    //将圆点上下移动
    y += yVel;
}

// 1 7 0 7 5 7 1 3 6 1 5 4 5 7 5 
// 4 6 0 7 1 8 8 6 6 8 8 8 4 1 1 
// 5 0 0 3 5 3 1 7 4 7 6 0 0 2 5 
// 4 5 2 2 3 2 1 1 8 8 0 5 5 4 4 
// 6 0 5 6 2 8 7 3 4 2 0 0 0 0 2 
// 6 2 5 6 5 7 6 6 8 5 3 6 2 8 1 
// 6 6 8 0 1 1 7 0 3 2 0 1 2 1 8 
// 3 5 2 6 0 7 2 7 2 8 1 6 5 1 5 
// 4 6 0 4 6 2 3 2 0 4 3 7 5 3 6 
// 5 4 2 5 2 1 3 2 8 3 2 0 0 7 2 
// 4 3 6 2 5 1 2 6 4 2 2 7 8 5 1 
// 5 1 4 8 4 6 7 5 8 6 0 8 4 0 7 
// 4 2 8 1 5 2 3 7 8 7 8 1 3 7 5 
// 2 3 6 6 0 2 1 7 7 8 2 5 7 7 4 
// 3 0 6 0 2 0 2 3 6 2 8 5 1 2 1 

void Tile::updateDraw(SDL_Surface *screen)
{
    SDL_Rect offset;
    int cs, ce;
    int rs, re;
    int i, j;

    if( camera->x < 0 ) camera->x = 0;
    if( (camera->x+camera->w) > (TILE_WIDTH*GRID_CAL) ) camera->x =  (TILE_WIDTH*GRID_CAL) - camera->w;
    if( camera->y < 0 ) camera->y = 0;
    if( (camera->y+camera->h) > (TILE_HEIGHT*GRID_RAW) ) camera->y =  (TILE_HEIGHT*GRID_RAW) - camera->h;

    cs = camera->x / TILE_WIDTH;
    rs = camera->y / TILE_HEIGHT;
    ce = (camera->x + camera->w) / TILE_WIDTH;
    re = (camera->y + camera->h) / TILE_HEIGHT;
    
    for( j = rs; j < re; j++ )
    {
        for( i = cs; i < ce; i++ )
        {
            offset.x =  (i-cs)*TILE_WIDTH;
            offset.y =  (j-rs)*TILE_HEIGHT;
            
            nowFrameIndex = grid[j][i];
            SDL_BlitSurface( background, &clips[nowFrameIndex], screen, &offset );
        }
    }
}


void Tile::setBackground(string filename)
{
    background = loadImage(filename);
}


void Tile::setPos(int x, int y)
{
    this->x = x;
    this->y = y;
}
void Tile::getPos(int &x, int &y)
{
    x = this->x;
    y = this->y;
}


void Tile::adaptPos(int w, int h)
{
    if( this->x < 0 )
    {
        this->x = 0;
    }
    else if( this->x > (w-TILE_WIDTH) )
    {
        this->x = w-TILE_WIDTH;
    }

    if( this->y < 0 )
    {
        this->y = 0;
    }
    else if( this->y > (h-TILE_HEIGHT) )
    {
        this->y = h-TILE_HEIGHT;
    }
}


void Tile::setClips()
{
    //剪切子图
    for ( int j = 0; j < TILE_RAW_COUNT; j++ )
    {
        for ( int i = 0; i < TILE_CAL_COUNT; i++ )
        {
            clips[ j*TILE_RAW_COUNT+i ].x = TILE_WIDTH * j;
            clips[ j*TILE_RAW_COUNT+i ].y = TILE_HEIGHT * i;
            clips[ j*TILE_RAW_COUNT+i ].w = TILE_WIDTH;
            clips[ j*TILE_RAW_COUNT+i ].h = TILE_HEIGHT;
        }
    }
}

SDL_Surface * Tile::loadImage( string filename ) 
{
    SDL_Surface* loadedImage = NULL;

    //加载图像
    loadedImage = IMG_Load( filename.c_str() );
    //如果图像优化成功
    if( loadedImage != NULL )
    {
        //映射关键色 0x26ea21
        Uint32 colorkey = SDL_MapRGB( loadedImage->format, 0x26, 0xea, 0x21 );
        //将所有颜色为（R 0, G 0xFF, B 0xFF）的像素设为透明。
        SDL_SetColorKey( loadedImage, SDL_SRCCOLORKEY, colorkey );
    }

    //返回优化后的表面
    return loadedImage;
}

void Tile::setCamera(SDL_Rect *camera)
{
    this->camera = camera;
}